(function () {
    var exports = this;
    var shuziDaxie = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二', '十三', '十四', '十五',
        '十六', '十七', '十八', '十九', '二十'];
    var strArr = ['自摸次数', '接炮次数', '点炮次数', '暗杠条数', '明杠条数'];
    var playNum = 0;
    var DataInfo = null;
    var playRound = 0;
    var $ = null;

    var ZongJiesuanLayer = cc.Layer.extendEX({
        onEnter: function () {
            cc.Layer.prototype.onEnter.call(this);
        },
        onExit: function () {
            cc.Layer.prototype.onExit.call(this);
            $ = null;
        },
        ctor: function (data) {
            this._super();
      
            var that = this;
            loadNodeCCS(majiang.res.ZongJiesuanLayer_json, this);

            $ = create$(this.getChildByName('Scene'));

            var mapId = data['map_id'];

            var players = data.players;
            playNum = players.length;
            DataInfo = data;
            var title = (gameData.mapName || (data['map_id']
                                              ? (gameData.mapId2Name[mapId]) || ''
                                              : ''));
            $('root.panel.lb_ts1').setString(timestamp2time(data['ts'], 'HH:MM:ss'));
            $('root.panel.lb_ts2').setString(timestamp2time(data['ts'], 'yyyy-mm-dd'));
            $('root.panel.lb_title').setString(title);
            $('root.panel.lb_name').setString(title);
            $('root.panel.lb_roomid').setString('房号 ' + gameData.roomId + ' 局数 ' + data['cur_round'] + '/' + data['total_round']);

            if ($('root.panel.fangzhu'))
                $('root.panel.fangzhu').setVisible(!data['is_daikai']);

            //打了多少局
            if (players[0]['scores']) {
                playRound = players[0]['scores'].length;
            }
            var maxDianpao = 0;
            var maxScore = 0;
            for (var i = 0; i < players.length; i++) {
                var player = players[i];
                if (maxDianpao < player['dianpao'])
                    maxDianpao = player['dianpao'];
                if (maxScore < player['score']) {
                    maxScore = player['score'];
                }
            }

            var share_str = '';
            var myTotalScore = 0;
            for (var i = 0; i < players.length; i++) {
                var player = players[i];
                var uid = player['uid'];
                var jiepao = player['jiepao'];
                var dianpao = player['dianpao'];
                var angang = player['angang'];
                var minggang = player['minggang'];
                var score = player['score'];
                var playerInfo = gameData.getPlayerInfoByUid(uid);
                if (!playerInfo)
                    continue;
                share_str += (',' + playerInfo.nickname + '【' + score + '】');

                var head = $('root.panel.row' + i + '.info.head');
                var lbNickname = $('root.panel.row' + i + '.info.lb_nickname');
                var lbUid = $('root.panel.row' + i + '.info.lb_uid');
                var lbScore = $('root.panel.row' + i + '.lb_score');
                var lbScore2 = $('root.panel.row' + i + '.lb_score_0');

                loadImageToSprite(playerInfo.headimgurl, head, true);
                lbNickname.setString(playerInfo.nickname);
                lbUid.setString('ID: ' + playerInfo.uid);

                if (window.inReview)
                    lbUid.setVisible(false);


                if (uid == gameData.uid) {
                    $('root.panel.row' + i + 'infobg').setTexture(majiang.res.result_bg1);
                } else {
                    $('root.panel.row' + i + 'infobg').setTexture(majiang.res.result_bg2);
                }

                if (score > 0) {
                    lbScore.setVisible(true);
                    lbScore.setString('+' + score);
                    lbScore2.setVisible(false);
                } else if (score < 0) {
                    lbScore2.setVisible(true);
                    lbScore2.setString(score);
                    lbScore.setVisible(false);
                } else {
                    lbScore.setVisible(true);
                    lbScore.setString(score);
                    lbScore2.setVisible(false);
                }


                $('root.panel.row' + i + '.zjps').setVisible(maxDianpao <= dianpao && dianpao > 0);
                $('root.panel.row' + i + '.dyj').setVisible(maxScore <= score && score > 0);

                if (player.uid == gameData.uid) {
                    myTotalScore = player.score;
                }
            }
            this.initScoreScrollView();
            if (players.length < 4) {
                $('root.panel.row3').setVisible(false);
            }
            if (players.length < 3) {
                $('root.panel.row2').setVisible(false);
            }

            /*
            playEffect(myTotalScore >= 0
                       ? 'vWin'
                       : 'vLose');
            */
            var btnShare = $('root.panel.btn_share');

            if (gameData.loginType == 'yk') {
                btnShare.setVisible(false);
            }

            if (window.inReview) {
                btnShare.setVisible(false);
                $('root.panel.btn_back').setPositionX(640);
            }

            TouchUtil.setOnclickListener(btnShare, function () {
                if (!cc.sys.isNative)
                    return;
                ShareUtil.SharePic(that);
            });

            // TouchUtil.setOnclickListener($('root.panel.btn_share_card'), function () {
            //     if (!cc.sys.isNative)
            //         return;
            //     WXUtils.shareCard(share_str);
            // });
            //
            // TouchUtil.setOnclickListener($('root.panel.btn_share_card'), function () {
            //     if (!cc.sys.isNative)
            //         return;
            //     WXUtils.shareCard(share_str);
            // });

            TouchUtil.setOnclickListener($('root.panel.btn_back'), function () {
                showBoard('HallBoard');
            });
            return true;
        },
        initScoreScrollView: function () {
            var that = this;
            for (var i = 0; i < playNum; i++) {
                var data = DataInfo['players'][i];
                var uid = data['uid'];
                var zimo = data['zimo'];
                var jiepao = data['jiepao'];
                var dianpao = data['dianpao'];
                var angang = data['angang'];
                var minggang = data['minggang'];
                var info = [zimo, jiepao, dianpao, angang, minggang];
                var scroll_view = $('root.panel.row' + i + '.score_sv');
                scroll_view.removeAllChildren();
                var layout = new ccui.Layout();
                var scrollH = 0;
                for (var j = 0; j < 5; j++) {
                    var str = strArr[j] + '          ' + info[j];
                    var text = new ccui.Text(str, 'res/fonts/FZZY.ttf', 25);
                    text.setTextColor(cc.color(88, 4, 4));
                    text.x = 10 + text.getContentSize().width / 2;
                    text.y = 0 - scrollH - text.getContentSize().height / 2;
                    layout.addChild(text);
                    if (j < data.length - 1) {
                        var sprite = new cc.Sprite('res/table/jiesuan/line.png');
                        layout.addChild(sprite);
                        sprite.y = 0 - scrollH - text.getContentSize().height - 8 - sprite.getContentSize().height / 2;
                        sprite.x = sprite.getContentSize().width / 2;
                    }
                    scrollH += text.getContentSize().height + 16;
                }
                scroll_view.setInnerContainerSize(cc.size(scroll_view.getContentSize().width, scrollH));
                scroll_view.addChild(layout);
                if (scrollH < scroll_view.getContentSize().height) {
                    layout.y = scroll_view.getContentSize().height;
                } else {
                    layout.y = scrollH;
                }
            }
        }
    });

    exports.ZongJiesuanLayer = ZongJiesuanLayer;
})(window);
